home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / DEMOS / ROTATEAS.ZIP / ROTATE3.ASM < prev    next >
Encoding:
Assembly Source File  |  1993-12-24  |  11.8 KB  |  321 lines

  1. ; Realtime Bitmap Rotation Routine
  2. ; Handles approx. 0.5 million pixels / second on a 386
  3. ; by Soeren Holstebroe (seap)
  4. ; no group but available if you are interested. (code / algorithms + music)
  5. ;
  6. ; Last edited: 10/10-1993
  7. ;
  8. ; I would really appreciate if you would send some of your own source-codes
  9. ; or/and anything you use this piece of code in or/and any improvement of
  10. ; this routine.
  11. ; Note: This is freeware. You may do with this code whatever you want, but
  12. ; hey! please leave a greeting in your program. You greet me, I greet you.
  13. ; If any problems turn up or if you want to exchange more sources or if you
  14. ; just want to say "Hi! nice/lame piece of code you have made" or if you
  15. ; actually used this piece of asm, please write.
  16. ;
  17. ; Write to:
  18. ; Internet: seap@diku.dk
  19. ; Fidonet : 2:230/159.16
  20. ; Snailnet: Hjortekærbakken 1
  21. ;           2800 Lyngby
  22. ;           Denmark
  23. ; NB! Don't write to the snailnet address too long after release-date.
  24.  
  25. .286
  26. cseg    segment
  27.         assume  cs:cseg;ds:dseg
  28.         org     0100h           ; stak
  29. start:  jmp     main
  30.  
  31.         oldmode DB ?
  32.  
  33. opengfx:
  34.                 mov     ah,0fh
  35.                 int     10h
  36.                 mov     oldmode,al
  37.                 mov     ah,0h
  38.                 mov     al,13h
  39.                 int     10h
  40.         ret
  41.  
  42. closegfx:
  43.                 mov     ah,0h
  44.                 mov     al,oldmode
  45.                 int     10h
  46.         ret
  47.  
  48. makemulstab:    xor     di,di
  49.                 xor     ax,ax
  50.                 mov     cx,(iwidth*iheight)
  51. mulsloop:       mov     mulstab [di],ax
  52.                 add     ax,iwidth
  53.                 inc     di
  54.                 inc     di
  55.                 loop    mulsloop
  56.                 ret
  57.  
  58.  
  59. retrace:        mov     dx,3dah         ; wait for vertical retrace
  60. ventl:          in      al,dx
  61.                 and     al,8
  62.                 jz      ventl
  63. venth:          in      al,dx
  64.                 and     al,8
  65.                 jne     venth
  66.                 ret
  67.  
  68.  
  69.  
  70. tempX           dw      0
  71. tempY           dw      0
  72. cosvx           dw      0
  73. sinvx           dw      0
  74. cosvy           dw      0
  75. sinvy           dw      0
  76. cosv            dw      0
  77. sinv            dw      0
  78.  
  79.  
  80. rotate          proc    near
  81.                 mov     bp,angle        ; bp = rotation-angle
  82.                 mov     ax,cos[bp]      ; get cos(bp) and sin(bp) (for speed)
  83.                 mov     cosv,ax
  84.                 mov     ax,sin[bp]
  85.                 mov     sinv,ax
  86.  
  87.                 mov     cx,iheight       ; image-iheight
  88.                 mov     bx,(-(iheight/2)); bx = -iheight/2
  89.                 mov     di,0
  90.  
  91.                 ; pre-calc all multiplications
  92.  
  93.                 mov     ax,-(iwidth/2)
  94.                 imul    cosv
  95.                 mov     cosvx,ax        ; cosvx = cos v * X
  96.  
  97.                 mov     ax,-(iwidth/2)
  98.                 imul    sinv
  99.                 mov     sinvx,ax        ; sinvx = sin v * X
  100.  
  101.                 mov     ax,-(iheight/2)
  102.                 imul    cosv
  103.                 mov     cosvy,ax        ; cosvy = cos v * Y
  104.  
  105.                 mov     ax,-(iheight/2)
  106.                 imul    sinv
  107.                 mov     sinvy,ax        ; sinvy = sin v * Y
  108.  
  109. @ry:            push    cx
  110.                 mov     cx,iwidth       ; image iwidth
  111.  
  112.                 ; dx = sin v * Y + cos v * X
  113.                 mov     dx,sinvy
  114.                 add     dx,cosvx
  115.  
  116.                 ; dx = cos v * Y - sin v * X
  117.                 mov     si,cosvy
  118.                 sub     si,sinvx
  119.  
  120.                 ; Place rotation origin in center of sourceimage
  121.                 add     dx,(iwidth/2)*256
  122.                 add     si,(iheight/2)*256
  123.  
  124. @rx:
  125.                                         ; Xo = sin v * Y + cos v * X
  126.                                         ; Yo = cos v * Y - sin v * X
  127.  
  128.  
  129.                 cmp     dx,(iwidth)*256   ; check if x is within source
  130.                 jae     blackout
  131.  
  132.                 cmp     si,(iwidth)*256   ; the same for y
  133.                 jae     blackout
  134.  
  135.  
  136.                 ; Get-pixel
  137.                 ; calculate source-image offset
  138. getpixel:
  139.                 mov     bx,si            ; y = y/128 (fixed point)
  140.                 xchg    bh,bl
  141.                 xor     bh,bh
  142.                 add     bx,bx
  143.                 mov     ax,mulstab[bx]   ; get y offset
  144.                 xor     bx,bx            ; x = x/256 (fixed point)
  145.                 mov     bl,dh
  146.                 add     bx,ax            ; si = source = iwidth * Yo + Xo
  147.                 mov     al,ds:image2[bx] ; al = pixel color
  148.                                          ; change the above line if
  149.                                          ; you want another image.
  150.                                          ; Remember to change iwidth and
  151.                                          ; iheight as well
  152.                 jmp     plot_pixel
  153. blackout:
  154.                 mov     al,0             ; black pixel
  155.  
  156. plot_pixel:
  157.                 mov     es:[di],al       ; display pixel
  158.                 inc     di               ; next dest. pixel
  159.                                          ; NOTE!!!!
  160.                                          ; This will run faster
  161.                                          ; if two colors are computed
  162.                                          ; before the video-card is
  163.                                          ; being accesed.
  164.                                          ; When two colors are computed
  165.                                          ; you can use a word-write
  166.                                          ; instead of the byte-write.
  167.                                          ; If you own a 386 DX, you can
  168.                                          ; even wait until you have
  169.                                          ; computed 4 colors before you
  170.                                          ; acces the video-ram.
  171.                                          ; I have made byte-writes
  172.                                          ; to make it more compatible, but...
  173.                                          ; Well, please mail me any
  174.                                          ; improvements.
  175.                 ; update tempX
  176.                 add     dx,cosv
  177.                                         ; bx = sin v * Y + cos v * X
  178.                 ; update tempY
  179.                 sub     si,sinv
  180.                                         ; si = cos v * Y - sin v * X
  181.  
  182.                 loop    @jrx
  183.  
  184.                 ; update screen coord
  185.                 add     di,(320-iwidth) ; next dest. line
  186.  
  187.                 ; update sinvy & cosvy
  188.                 mov     ax,cosv
  189.                 add     cosvy,ax
  190.                 mov     ax,sinv
  191.                 add     sinvy,ax
  192.  
  193.                 pop     cx
  194.                 loop    @jry
  195.                 ret
  196.  
  197. ; the far jumps below can easily be removed to gain speed.
  198. ; They are only there for compatibility (gosh, that word was hard to spell).
  199.  
  200. @jrx:           jmp     @rx
  201. @jry:           jmp     @ry
  202.  
  203. rotate          endp
  204.  
  205.  
  206. main:
  207.  
  208.                 mov     ah,0ch          ; kill keyboard buffer
  209.                 mov     al,-1
  210.                 int     21h
  211.  
  212.                 call    opengfx         ; open mode 13
  213.                                         ; precalculate image-y-offsets
  214.                 call    makemulstab
  215.                 push    cs
  216.                 pop     ds
  217.                 mov     ax,0a000h
  218.                 mov     es,ax
  219.  
  220. mloop:
  221.                 call    rotate
  222.                 add     angle,2
  223.                 and     angle,1feh
  224.                 ; Angle goes from 0 to 255 degree. Remember to double
  225.                 ; the offset. (for wordsize).
  226.  
  227.                 call    retrace ; wait for v-blank
  228.                 mov     ah,1    ; check if kb-buffer is empty
  229.                 int     16h
  230.                 jz      mloop   ; nope, loop again
  231.  
  232.                 call    closegfx
  233.  
  234.                 mov     ah,4ch          ; return to dos
  235.         int    21h
  236.  
  237.                 mulstab DW (iwidth*iheight) DUP(?)
  238.  
  239.                 angle  dw      0            ; rotation angle
  240.                 iwidth   = 100              ; image width
  241.                 iheight  = 100              ; image height
  242.                                             ; the two constants above
  243.                                             ; can easily be changed
  244.                                             ; into declarations.
  245. ; test-pattern nr. 1
  246.  
  247. image1  db      0,0,0,0,12,14,15,16,17,18,0,0,0,0,0,0
  248.         db      0,0,0,11,13,19,0,0,0,20,21,22,0,0,0,0
  249.         db      0,0,9,10,0,0,0,0,0,0,23,24,0,0,0,0
  250.         db      0,0,8,0,0,0,0,0,0,0,0,25,0,0,0,0
  251.         db      0,0,7,6,0,0,0,0,0,0,0,26,27,0,0,0
  252.         db      0,0,0,5,4,0,0,0,0,0,0,28,29,0,0,0
  253.         db      0,0,0,0,3,2,1,0,0,0,0,30,31,0,0,0
  254.         db      0,0,0,0,0,0,0,0,0,0,32,33,0,0,0,0
  255.         db      0,0,0,0,0,0,0,0,0,34,35,36,0,0,0,0
  256.         db      0,0,0,0,0,0,0,0,37,38,39,0,0,0,0,0
  257.         db      0,0,0,0,0,0,0,40,41,42,43,0,0,0,0,0
  258.         db      0,0,0,0,0,0,44,45,46,47,0,0,0,0,0,0
  259.         db      0,0,0,0,48,49,50,51,52,0,0,0,0,0,0,0
  260.         db      0,0,0,53,54,55,56,57,0,0,0,0,0,0,0,0
  261.         db      0,0,58,59,60,61,62,63,0,0,0,0,0,0,0,0
  262.         db      0,64,65,66,67,68,69,70,71,0,0,0,0,0,0,0
  263.  
  264. ; test-pattern nr. 2 (compiles a bit slow)
  265.  
  266. image2  label byte
  267.         rept    (100*25)
  268.         db      0,0,0,40
  269.         endm
  270.  
  271. ; precalculated sin/cos table
  272. ; table-size: 256+64=320
  273.  
  274.  
  275. sin     DW      00000h,00006h,0000ch,00012h,00018h,0001fh,00025h,0002bh
  276.     DW    00031h,00037h,0003dh,00044h,0004ah,0004fh,00055h,0005bh
  277.     DW    00061h,00067h,0006dh,00072h,00078h,0007dh,00083h,00088h
  278.     DW    0008dh,00092h,00097h,0009ch,000a1h,000a6h,000abh,000afh
  279.     DW    000b4h,000b8h,000bch,000c1h,000c5h,000c9h,000cch,000d0h
  280.     DW    000d4h,000d7h,000dah,000ddh,000e0h,000e3h,000e6h,000e9h
  281.     DW    000ebh,000edh,000f0h,000f2h,000f4h,000f5h,000f7h,000f8h
  282.     DW    000fah,000fbh,000fch,000fdh,000fdh,000feh,000feh,000feh
  283. cos     DW      000ffh,000feh,000feh,000feh,000fdh,000fdh,000fch,000fbh
  284.     DW    000fah,000f8h,000f7h,000f5h,000f4h,000f2h,000f0h,000edh
  285.     DW    000ebh,000e9h,000e6h,000e3h,000e0h,000ddh,000dah,000d7h
  286.     DW    000d4h,000d0h,000cch,000c9h,000c5h,000c1h,000bch,000b8h
  287.     DW    000b4h,000afh,000abh,000a6h,000a1h,0009ch,00097h,00092h
  288.     DW    0008dh,00088h,00083h,0007dh,00078h,00072h,0006dh,00067h
  289.     DW    00061h,0005bh,00055h,0004fh,0004ah,00044h,0003dh,00037h
  290.     DW    00031h,0002bh,00025h,0001fh,00018h,00012h,0000ch,00006h
  291.     DW    00000h,0fffah,0fff4h,0ffeeh,0ffe8h,0ffe1h,0ffdbh,0ffd5h
  292.     DW    0ffcfh,0ffc9h,0ffc3h,0ffbch,0ffb6h,0ffb1h,0ffabh,0ffa5h
  293.     DW    0ff9fh,0ff99h,0ff93h,0ff8eh,0ff88h,0ff83h,0ff7dh,0ff78h
  294.     DW    0ff73h,0ff6eh,0ff69h,0ff64h,0ff5fh,0ff5ah,0ff55h,0ff51h
  295.     DW    0ff4ch,0ff48h,0ff44h,0ff3fh,0ff3bh,0ff37h,0ff34h,0ff30h
  296.     DW    0ff2ch,0ff29h,0ff26h,0ff23h,0ff20h,0ff1dh,0ff1ah,0ff17h
  297.     DW    0ff15h,0ff13h,0ff10h,0ff0eh,0ff0ch,0ff0bh,0ff09h,0ff08h
  298.     DW    0ff06h,0ff05h,0ff04h,0ff03h,0ff03h,0ff02h,0ff02h,0ff02h
  299.     DW    0ff02h,0ff02h,0ff02h,0ff02h,0ff03h,0ff03h,0ff04h,0ff05h
  300.     DW    0ff06h,0ff08h,0ff09h,0ff0bh,0ff0ch,0ff0eh,0ff10h,0ff13h
  301.     DW    0ff15h,0ff17h,0ff1ah,0ff1dh,0ff20h,0ff23h,0ff26h,0ff29h
  302.     DW    0ff2ch,0ff30h,0ff34h,0ff37h,0ff3bh,0ff3fh,0ff44h,0ff48h
  303.     DW    0ff4ch,0ff51h,0ff55h,0ff5ah,0ff5fh,0ff64h,0ff69h,0ff6eh
  304.     DW    0ff73h,0ff78h,0ff7dh,0ff83h,0ff88h,0ff8eh,0ff93h,0ff99h
  305.     DW    0ff9fh,0ffa5h,0ffabh,0ffb1h,0ffb6h,0ffbch,0ffc3h,0ffc9h
  306.     DW    0ffcfh,0ffd5h,0ffdbh,0ffe1h,0ffe8h,0ffeeh,0fff4h,0fffah
  307.     DW    00000h,00006h,0000ch,00012h,00018h,0001fh,00025h,0002bh
  308.     DW    00031h,00037h,0003dh,00044h,0004ah,0004fh,00055h,0005bh
  309.     DW    00061h,00067h,0006dh,00072h,00078h,0007dh,00083h,00088h
  310.     DW    0008dh,00092h,00097h,0009ch,000a1h,000a6h,000abh,000afh
  311.     DW    000b4h,000b8h,000bch,000c1h,000c5h,000c9h,000cch,000d0h
  312.     DW    000d4h,000d7h,000dah,000ddh,000e0h,000e3h,000e6h,000e9h
  313.     DW    000ebh,000edh,000f0h,000f2h,000f4h,000f5h,000f7h,000f8h
  314.     DW    000fah,000fbh,000fch,000fdh,000fdh,000feh,000feh,000feh
  315.  
  316. cseg            ends
  317.                 END start
  318.  
  319. ; seap was here :)
  320.  
  321.